//
//  MainWindow.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/10/9.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class MainWindow: NSWindow {
    
    var identifier_navtiveControl = NSToolbarItem.Identifier.init(rawValue: "identifier_navtiveControl")
    var identifier_customTitle = NSToolbarItem.Identifier.init(rawValue: "identifier_customTitle")
    
    lazy var controller: MainViewController? = {
        let controller = self.contentViewController as? MainViewController
        return controller
    }()
    
    override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
        self.title = ""
        self.isMovableByWindowBackground = true
        self.titleVisibility = .hidden
        self.toolbar = self.customToolbar
        
        self.styleMask = [.titled,.closable,.miniaturizable]
        self.standardWindowButton(.zoomButton)?.isHidden = true
        self.closeButton?.action = #selector(windowClose)
    }
    
    lazy var closeButton: NSButton? = {
        let closeButton = self.standardWindowButton(NSWindow.ButtonType.closeButton)
        closeButton?.target = self
        return closeButton
    }()
    
    lazy var customToolbar: NSToolbar = {
        let tool = NSToolbar.init(identifier: "MainWindow")
        tool.allowsUserCustomization = true
        tool.autosavesConfiguration = true
        tool.displayMode = .iconOnly
        tool.sizeMode = .small
        tool.delegate = self
        return tool
    }()
    lazy var customTitle: NSLabel = {
        let lab = NSLabel.label(title: "OneMeeting", textColor: color_gray(0), font: NSFont.systemFont(ofSize: 15))
        lab.sizeToFit()
        lab.alignment = .center
        return lab
    }()
    
    lazy var nactiveSegment: NSSegmentedControl = {
        let images = [
            NSImage.image(name:"icon_home_refresh")!,
            NSImage.image(name:"icon_home_Introduction")!,
            NSImage.image(name:"icon_home_more")!
        ]
        let view = NSSegmentedControl.init(images: images, trackingMode: NSSegmentedControl.SwitchTracking.momentary, target: self, action: #selector(segmentedControlActon(sender:)))
        return view
    }()
}

extension MainWindow {
    @objc func segmentedControlActon(sender:NSSegmentedControl) {
        switch sender.selectedSegment {
        case 0:
            self.controller?.reLoadHost(success: nil)
        case 1:
            self.controller?.introduction(sender: sender)
        case 2:
            self.controller?.moreAction(sender: sender)
        default:
            break
        }
    }
    
    @objc func windowClose() {
        NSApp.terminate(nil)
    }
}


extension MainWindow:NSToolbarDelegate {
    func toolbarItemIdentifiers() -> [NSToolbarItem.Identifier] {
        return [
            NSToolbarItem.Identifier.space,
            NSToolbarItem.Identifier.space,
            NSToolbarItem.Identifier.flexibleSpace,
            identifier_customTitle,
            NSToolbarItem.Identifier.flexibleSpace,
            identifier_navtiveControl,
        ]
    }
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return self.toolbarItemIdentifiers()
    }
    
    func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return self.toolbarItemIdentifiers()
    }
    
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        let toolbarItem = NSToolbarItem.init(itemIdentifier: itemIdentifier)
        if itemIdentifier.rawValue.isEqual(identifier_navtiveControl.rawValue) {
            toolbarItem.view = self.nactiveSegment
            return toolbarItem
        }else if itemIdentifier.rawValue.isEqual(identifier_customTitle.rawValue)  {
            toolbarItem.view = self.customTitle
            return toolbarItem
        }else {
            return nil
        }
        
    }
}

